import 'package:banyou_app/app/modules/dongtai/controllers/dongtai_controller.dart';
import 'package:banyou_app/app/modules/mine/controllers/mine_controller.dart';
import 'package:banyou_app/app/modules/xiangqu/controllers/xiangqu_controller.dart';
import 'package:banyou_app/app/routes/app_pages.dart';
import 'package:banyou_app/app/util/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class TabbatController extends GetxController {
  final currentIndex = 0.obs;
  final showTabbar = true.obs;
  final tab = 0.obs; //值为1默认将#H5动态页 变为第一个 tab，并显示#H5动态页，值为0显示原生的首页
  final PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    dynamic ttab = box.read("tab");
    if (ttab != null && ttab == 1) {
      changePage(2);
    } else {
      changePage(ttab);
    }
    super.onReady();
  }

  @override
  void onClose() {}

  void changePage(int index) {
    String key = box.read("key") ?? "";
    if (index == 2) {
      if (key.isEmpty) {
        Get.offAllNamed(Routes.LOGIN);
      } else {
        currentIndex.value = index;
        pageController.jumpToPage(index);
        Get.find<DongtaiController>().loadUrl();
      }
    } else if (index == 1) {
      // if (key.isEmpty) {
      //   Get.offAllNamed(Routes.LOGIN);
      // } else {
      // Get.find<XiangquController>().loadData();
      currentIndex.value = index;
      pageController.jumpToPage(index);
      // }
    } else if (index == 3) {
      if (key.isEmpty) {
        Get.offAllNamed(Routes.LOGIN);
      } else {
        Get.find<MineController>().loadData();
        currentIndex.value = index;
        pageController.jumpToPage(index);
      }
    } else {
      currentIndex.value = index;
      pageController.jumpToPage(index);
    }
    //
    // dynamic ttab = box.read("tab");
    bool qidong = box.read("qidong");
    // 弹框
    dynamic rank = box.read("rank");
    if ((rank.toString() == '1' && qidong == true)) {
      box.write("qidong", false);
      channel.invokeMethod('toscore');
    }
  }
}
